package com.gy.assistant.ui.friend.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import im.zego.zim.entity.ZIMFriendApplicationInfo;
import im.zego.zim.enums.ZIMFriendApplicationState;
import im.zego.zim.enums.ZIMFriendApplicationType;
import com.gy.assistant.R;
import java.util.ArrayList;
import java.util.List;

public class FriendRequestListAdapter extends RecyclerView.Adapter {

    private OnItemClickListener onItemClickListener;
    private final List<ZIMFriendApplicationInfo> friendApplicationInfos = new ArrayList<>();

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

                View view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_friend_application_content, parent, false);
        return new ZIMFriendViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ZIMFriendViewHolder) {
            final ZIMFriendViewHolder zimFriendViewHolder = (ZIMFriendViewHolder)holder;

            ZIMFriendApplicationInfo friendApplicationInfo = friendApplicationInfos.get(position);
            zimFriendViewHolder.setZIMFriendApplicationInfo(friendApplicationInfo);

            if (onItemClickListener != null) {
                zimFriendViewHolder.setOnClickListener(onItemClickListener);
            }
        }
    }

    @Override
    public int getItemCount() {
        return friendApplicationInfos.size();
    }

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.onItemClickListener = mOnItemClickListener;
    }

    @SuppressLint("NotifyDataSetChanged")
    public void addAll(ArrayList<ZIMFriendApplicationInfo> friendApplicationInfos) {
       this.friendApplicationInfos.clear();
        this.friendApplicationInfos.addAll(friendApplicationInfos);
        notifyDataSetChanged();
    }

    @SuppressLint("NotifyDataSetChanged")
    public void add(ArrayList<ZIMFriendApplicationInfo> friendApplicationInfos) {
        this.friendApplicationInfos.addAll(friendApplicationInfos);
        notifyDataSetChanged();
    }

    @SuppressLint("NotifyDataSetChanged")
    public void delete(ArrayList<ZIMFriendApplicationInfo> addFriendApplicationInfos) {
        List<ZIMFriendApplicationInfo> tmpFriendApplicationInfos = friendApplicationInfos;
        for (int i = 0; i < tmpFriendApplicationInfos.size(); i++) {
            for (int d = 0; d < addFriendApplicationInfos.size(); d++) {
                if (tmpFriendApplicationInfos.get(i).applyUser.userID.equals(
                        friendApplicationInfos.get(d).applyUser.userID)) {
                    friendApplicationInfos.remove(i);
                }
            }
        }
        notifyDataSetChanged();
    }

    @SuppressLint("NotifyDataSetChanged")
    public void update(ArrayList<ZIMFriendApplicationInfo> updateFriendApplicationInfos) {
        List<ZIMFriendApplicationInfo> tmpFriendList = friendApplicationInfos;
        for (int i = 0; i < tmpFriendList.size(); i++) {
            for (int d = 0; d < updateFriendApplicationInfos.size(); d++) {
                if (tmpFriendList.get(i).applyUser.userID.equals(
                        updateFriendApplicationInfos.get(d).applyUser.userID)) {
                    friendApplicationInfos.set(i, updateFriendApplicationInfos.get(d));
                }
            }
        }
        notifyDataSetChanged();
    }

    public interface OnItemClickListener {
        void onItemAcceptClick(ZIMFriendApplicationInfo friendApplicationInfo);

        void onItemRefuseClick(ZIMFriendApplicationInfo friendApplicationInfo);
    }

    public static class ZIMFriendViewHolder extends RecyclerView.ViewHolder {
        TextView userNameTextView;
        ImageView avatarImageView;
        RelativeLayout list;

        TextView tvReaction;
        TextView txWording;

        TextView txState;

        Button btAccept;
        Button btRefuse;

        View buttonPanel;

        public ZIMFriendViewHolder(View itemView) {
            super(itemView);
            list = itemView.findViewById(R.id.list);
            userNameTextView = itemView.findViewById(R.id.user_name);
            avatarImageView = itemView.findViewById(R.id.avatar);
            txWording = itemView.findViewById(R.id.wording);
            txState = itemView.findViewById(R.id.state);
            buttonPanel = itemView.findViewById(R.id.buttonPanel);
            btRefuse = itemView.findViewById(R.id.refuse);
            btAccept = itemView.findViewById(R.id.accept);
        }

        public OnItemClickListener listener;

        public void setZIMFriendApplicationInfo(ZIMFriendApplicationInfo friendApplicationInfo) {
            String userName = friendApplicationInfo.applyUser.userName;
            if (!"".equals(userName)) {
                userNameTextView.setText(userName);
            } else if (!"".equals(friendApplicationInfo.applyUser.userID)) {
                userNameTextView.setText(friendApplicationInfo.applyUser.userID);
            }
            txWording.setText(friendApplicationInfo.wording);
            if (friendApplicationInfo.type == ZIMFriendApplicationType.RECEIVED) {
                buttonPanel.setVisibility(View.VISIBLE);
            } else if (friendApplicationInfo.type == ZIMFriendApplicationType.SENT) {
                buttonPanel.setVisibility(View.GONE);
            } else if (friendApplicationInfo.type == ZIMFriendApplicationType.BOTH) {
                buttonPanel.setVisibility(View.VISIBLE);
            }

            if (friendApplicationInfo.state == ZIMFriendApplicationState.ACCEPTED) {
                buttonPanel.setVisibility(View.GONE);
            }

            btRefuse.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onItemRefuseClick(friendApplicationInfo);
                }
            });

            btAccept.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onItemAcceptClick(friendApplicationInfo);
                }
            });

            txState.setText(friendApplicationInfo.state.name());
            Glide.with(this.avatarImageView.getContext())
                .load(friendApplicationInfo.applyUser.userAvatarUrl)
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))
                .into(avatarImageView);
        }

        public void setOnClickListener(OnItemClickListener onItemClickListener) {
            listener = onItemClickListener;
        }
    }
}
